/*
 * $Id: LocationModelToLocationLightDTModelConverter.java 44 2013-04-23 09:00:37Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 44 $
 * $Date: 2013-04-23 09:00:37 +0000 (Tue, 23 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support.converter;

import com.sci.logisticsmap.data.model.LocationModel;
import com.sci.logisticsmap.data.model.location.DCModel;
import com.sci.logisticsmap.data.model.location.DeliveryModel;
import com.sci.logisticsmap.data.model.location.PickupModel;
import com.sci.logisticsmap.data.model.location.ReturnModel;
import com.sci.logisticsmap.service.model.LocationLightDTModel;
import com.sci.logisticsmap.service.model.location.DCLightDTModel;
import com.sci.logisticsmap.service.model.location.DeliveryLightDTModel;
import com.sci.logisticsmap.service.model.location.PickupLightDTModel;
import com.sci.logisticsmap.service.model.location.ReturnLightDTModel;
import com.sci.logisticsmap.support.converter.BaseConverter;
import com.sci.logisticsmap.support.model.geocoding.DirectionsLeg;
import com.sci.logisticsmap.support.model.geocoding.DirectionsRoute;
import org.springframework.stereotype.Component;

/**
 * The class {@code LocationModelToLocationLightDTModelConverter} represents the converter
 * from {@link LocationModel} to {@link LocationLightDTModel}.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 44 $
 */
@Component
public class LocationModelToLocationLightDTModelConverter extends BaseConverter<LocationModel, LocationLightDTModel> {
    /*
     * (non-Javadoc)
     *
     * @see BaseConverter#convert(Object)
     */
    public LocationLightDTModel convert(LocationModel source) {
        LocationLightDTModel target;
        if (source instanceof DCModel) {
            target = new DCLightDTModel();
        } else if (source instanceof DeliveryModel) {
            target = new DeliveryLightDTModel();
        } else if (source instanceof PickupModel) {
            target = new PickupLightDTModel();
        } else if (source instanceof ReturnModel) {
            target = new ReturnLightDTModel();
        } else {
            throw new IllegalStateException("Unsupported location type.");
        }
        target.setId(source.getId());
        target.setLabel(source.getLabel());
        target.setAddress(source.getAddress());
        target.setCity(source.getCity());
        target.setState(source.getState());
        target.setZipCode(source.getZipCode());
        target.setCountry(source.getCountry());
        if (source.getLatLng() != null) {
            target.setLatLng(source.getLatLng());
        }
        if (source.getRoutes() != null) {
            int duration = 0, distance = 0;
            DirectionsRoute route = source.getRoutes().get(0);
            if (route.getLegs() != null) {
                for (DirectionsLeg leg : route.getLegs()) {
                    duration += leg.getDuration().getValue();
                    distance += leg.getDistance().getValue();
                }
            }
            if (duration > 0) target.setDuration(duration);
            if (distance > 0) target.setDistance(distance);
        }
        target.setPosition(source.getPosition());
        target.setStatus(source.getStatus());
        target.setDeleted(source.isDeleted());
        return target;
    }
}
